// miniprogram/pages/main/main.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bookinglist: [],
    replytexts:'',
    modalHidden: true,

  },
  onLoad: function (options) {  
  },
  onReady: function () {
    //用户根据自己的openid查询预约
    //判断是否授权
    if (app.globalData.openid != null && app.globalData.openid!=""){
      const db = wx.cloud.database()
      //查询预约
      db.collection("booking").where({
        _openid: app.globalData.openid
      })
        .orderBy('dates', 'desc')
        .orderBy('times', 'desc')
        .get({
          success: res => {
            this.setData({
              bookinglist: res.data
            })
          }, fail: err => {
            wx.showToast({
              icon: "none",
              title: '网络异常，请重试',
            })
          }
        })
    }else{
      wx.showToast({
        icon: "none",
        title: '还未授权该小程序',
      })
    }
    
  },
  //打开查看消息
  check:function(e){
    //已查看,标记用户读
    const db = wx.cloud.database()
    console.log(e.target.dataset.id)
    db.collection("booking").doc(e.target.dataset.id).update({
      data: {
        isread: 1,
      }, success: res => {
      }, fail: err => {
      }
    })
    //弹出信息框
    this.setData({
      replytexts: e.target.dataset.name,
      modalHidden: !this.data.modalHidden,
    })
  },
  //点击关闭
  modalBindaconfirm: function (e) {
    this.setData({
      modalHidden: !this.data.modalHidden,
    })
    getCurrentPages()[getCurrentPages().length - 1].onReady()
  },
  //取消返回
  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
    })
    getCurrentPages()[getCurrentPages().length - 1].onReady()
  },
  //取消
  tocancel: function (e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          let id = e.currentTarget.dataset.id
          const db = wx.cloud.database();
          db.collection("booking").doc(id).remove({
            success: res => {
              
              wx.showToast({
                title: '删除成功',
              })
              //删除成功后刷新当前页面
              getCurrentPages()[getCurrentPages().length - 1].onReady()
              //删除成功重新加载
            }, fail: err => {
              wx.showToast({
                title: '删除失败',
              })
            }
          })
        }
      }
    })
  },
  
})